package top.hidoctor.auscultation.feature.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.CountDownTimer;

import com.tang.util.AppUtil;
import com.tang.util.DialogUtil;
import com.tang.util.GsonUtil;

import top.hidoctor.auscultation.MainActivity;
import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.base.Constant;
import top.hidoctor.auscultation.net.c.LoginContract;
import top.hidoctor.auscultation.net.req.LoginReq;
import top.hidoctor.auscultation.net.p.LoginPresenter;
import top.hidoctor.auscultation.net.req.RegisterReq;
import top.hidoctor.auscultation.net.req.SendEmsReq;

public class LoginActivity extends BaseActivity implements View.OnClickListener, LoginContract.View {

    private TextView tvLogin, tvRegister, tvCode;
    private EditText editPhone, editPassword, editCode;
    private LinearLayout llCode, llBottom;
    private View codeView;
    private Button button;
    private ImageView ivWechat;
    private String type = Constant.LOGIN_TYPE;
    private Timer timer;
    private LoginPresenter presenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initEvent();
    }


    private void initEvent() {
        presenter = new LoginPresenter(this, this);
        timer = new Timer(60000, 1000);
    }

    private void initView() {
        tvLogin = findViewById(R.id.tv_login);
        tvRegister = findViewById(R.id.tv_register);
        editPhone = findViewById(R.id.edit_phone);
        editPassword = findViewById(R.id.edit_password);
        editCode = findViewById(R.id.edit_code);
        llCode = findViewById(R.id.ll_code);
        codeView = findViewById(R.id.ll_code_view);
        button = findViewById(R.id.btn);
        ivWechat = findViewById(R.id.iv_wechat);
        llBottom = findViewById(R.id.ll_bottom);
        tvCode = findViewById(R.id.tv_send_code);


        tvLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        tvCode.setOnClickListener(this);
        button.setOnClickListener(this);

    }

    /**
     * 点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                if (type.equals(Constant.LOGIN_TYPE)) {
                    return;
                }
                switchLoginOrRegiste();
                break;
            case R.id.tv_register:
                if (type.equals(Constant.REGISTE_TYPE)) {
                    return;
                }
                switchLoginOrRegiste();
                break;
            case R.id.tv_send_code:
                if (!AppUtil.isFastClick()) {
                    String phone = editPhone.getText().toString();
                    if (TextUtils.isEmpty(phone)) {
                        showToast("请输入手机号");
                        return;
                    }
                    SendEmsReq req = new SendEmsReq();
                    req.setPhone(phone);
                    req.setType("0");
                    presenter.sendSmsCode(req);
                }
                break;
            case R.id.btn:
                if (!AppUtil.isFastClick()) {
                    String phone = editPhone.getText().toString();
                    String password = editPassword.getText().toString();
                    String code = editCode.getText().toString();
                    if (TextUtils.isEmpty(phone)) {
                        showToast("请输入手机号");
                        return;
                    }
                    if (TextUtils.isEmpty(password)) {
                        showToast("请输入密码");
                        return;
                    }
                    if (button.getText().equals("登录")) {
                        LoginReq req = new LoginReq();
                        req.setLoginName(phone);
                        req.setPassword(password);
                        req.setLoginType("2");
                        req.setScope("1");
                        presenter.login(req);
                    } else {
                        if (TextUtils.isEmpty(code)) {
                            showToast("请输入验证码");
                            return;
                        }
                        RegisterReq req = new RegisterReq();
                        req.setPhone(phone);
                        req.setPassword(password);
                        req.setCode(code);
                        presenter.register(req);
                    }
                }
                break;
        }
    }

    /**
     * 登陆 注册切换
     */
    private void switchLoginOrRegiste() {
        if (Constant.LOGIN_TYPE.equals(type)) {
            type = Constant.REGISTE_TYPE;
            llCode.setVisibility(View.VISIBLE);
            codeView.setVisibility(View.VISIBLE);
            button.setText("注册");
            llBottom.setVisibility(View.GONE);
            tvRegister.setTextColor(getResources().getColor(R.color.text_back));
            tvLogin.setTextColor(getResources().getColor(R.color.gray_text));
        } else if (Constant.REGISTE_TYPE.equals(type)) {
            type = Constant.LOGIN_TYPE;
            llCode.setVisibility(View.GONE);
            codeView.setVisibility(View.GONE);
            button.setText("登录");
            llBottom.setVisibility(View.VISIBLE);
            tvRegister.setTextColor(getResources().getColor(R.color.gray_text));
            tvLogin.setTextColor(getResources().getColor(R.color.text_back));
        }
    }

    /**
     * 验证码发送成功回调
     */
    @Override
    public void sendSmsSuc() {
        tvCode.setClickable(false);
        timer.start();
    }

    /**
     * 登陆成功回调
     */
    @Override
    public void loginSuc() {
        showToast("登录成功");
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }

    /**
     * 注册成功回调
     */
    @Override
    public void registerSuc() {
        showToast("注册成功，请登录");
        switchLoginOrRegiste();
    }

    /**
     * 未完善信息回调
     * @param userId
     */
    @Override
    public void completeInfo(final String userId) {
        DialogUtil.createAlertDialog(this, "", "您的信息未完善，请前往补充完善信息以正常使用功能？", "确定", "取消", false, new DialogUtil.DialogButtonClickListener() {
            @Override
            public void onPositiveButtonClick(DialogInterface dialog) {
                showToast("请完善信息");
                startActivity(new Intent(LoginActivity.this, CompleteInfoAct.class).putExtra("userId", userId));
            }

            @Override
            public void onNegativeButtonClick(DialogInterface dialog) {
                dialog.dismiss();
            }
        }).show();

    }

    /**
     * 验证码 倒计时
     */
    class Timer extends CountDownTimer {

        public Timer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (this != null) {
                tvCode.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.text_gray_edit));
                tvCode.setBackground(null);
                tvCode.setText((millisUntilFinished / 1000) + "s");
            }
        }

        @Override
        public void onFinish() {
            tvCode.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.colorPrimary));
            tvCode.setBackground(ContextCompat.getDrawable(LoginActivity.this, R.drawable.bg_blue_8));
            tvCode.setText("获取验证码");
            tvCode.setClickable(true);
        }
    }
}
